package com.spider.gp.processor;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spider.gp.dao.ZhiShu;
import com.spider.gp.dao.ZhiShuDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * description: f14-指数名称 f2/100 指数 f3/100 涨跌幅点 f4/100 涨跌幅点数 f6/1000/1000 成交量  f104 上涨家数 f105 下跌家数  f106 平家
 *
 * @author clare
 * @date 2022/05/20 08:56
 **/
@Component
public class ZhishuProcessor implements PageProcessor {
    private Site site = Site.me().setRetryTimes(3).setSleepTime(1000);

    @Autowired
    private ZhiShuDao zhiShuDao;

    @Override
    public void process(Page page) {
        // f14-指数名称 f2/100 指数 f3/100 涨跌幅点 f4/100  % 涨跌幅点数 f6/10000/10000 成交量  f104 上涨家数 f105 下跌家数  f106 平家
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String reportTime = formatter.format(new Date());
        LambdaQueryWrapper<ZhiShu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ZhiShu::getReportTime, reportTime);
        ZhiShu zhiShu = zhiShuDao.selectOne(wrapper);
        if (zhiShu == null) {
            zhiShuDao.insert(ZhiShu.builder().reportTime(reportTime).build());
        }

        StringBuffer stateB = new StringBuffer();
        boolean zflag = true;
        BigDecimal totalCjl = new BigDecimal(0);
        List<String> all = page.getJson().jsonPath("$.data.diff").all();
        for (String item : all) {
            HashMap hashMap = JSON.parseObject(item, HashMap.class);
            String zsmc = hashMap.get("f14").toString();
            String szjs = hashMap.get("f104").toString();
            String xdjs = hashMap.get("f105").toString();
            String pj = hashMap.get("f106").toString();
            BigDecimal zs = new BigDecimal(hashMap.get("f2").toString()).divide(BigDecimal.valueOf(100));
            BigDecimal zdf = new BigDecimal(hashMap.get("f3").toString()).divide(BigDecimal.valueOf(100));
            BigDecimal cjl = new BigDecimal(hashMap.get("f6").toString()).divide(BigDecimal.valueOf(10000)).divide(BigDecimal.valueOf(10000));

            StringBuffer jia = new StringBuffer();
            jia.append(szjs).append("|").append(pj).append("|").append(xdjs);


            if (zsmc.equals("上证指数")) {
                totalCjl = totalCjl.add(cjl);
                if (zdf.toString().contains("-")) {
                    zflag = false;
                }
                zhiShuDao.updateById(ZhiShu.builder().reportTime(reportTime)
                        .szzsJia(jia.toString()).szzsZf(zs.toString()).szzsZfd(zdf.toString()  + "%").szzsCjl(cjl.setScale(2, BigDecimal.ROUND_HALF_UP).toString() + "亿")
                        .build());
            }
            if (zsmc.equals("深证成指")) {
                totalCjl = totalCjl.add(cjl);
                if (zdf.toString().contains("-")) {
                    zflag = false;
                }
                zhiShuDao.updateById(ZhiShu.builder().reportTime(reportTime)
                        .szczJia(jia.toString()).szczZf(zs.toString()).szczZfd(zdf.toString()  + "%").szczCjl(cjl.setScale(2, BigDecimal.ROUND_HALF_UP).toString() + "亿")
                        .build());
            }
        }

        if (zflag) {
            stateB.append("1");
        } else {
            stateB.append("0");
        }

        if (totalCjl.compareTo(new BigDecimal(10000)) >= 1) {
            stateB.append("1");
        } else {
            stateB.append("0");
        }
        zhiShuDao.updateById(ZhiShu.builder().reportTime(reportTime)
                .state(stateB.toString())
                .build());
    }

    @Override
    public Site getSite() {
        return site;
    }
}








